package factory.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dataStandardCatalog.domain.DataStandardCatalog;
import dataStandardCatalog.dto.AddDataStandardCatalogDTO;
import dataStandardCatalog.dto.DataStandardCatalogDTO;
import dataStandardCatalog.vo.ClickOnStandardLableVO;
import dataStandardCatalog.vo.DataStandardCatalogVO;
import databaseManageInfo.domain.DatabaseManageInfo;
import factory.mapper.DataStandardCatalogMapper;
import factory.service.DataStandardCatalogService;
import org.springframework.stereotype.Service;
import result.PageDTO;
import utils.BeanConverter;

import java.time.LocalDateTime;

@Service
public class DataStandardCatalogServiceImpl extends ServiceImpl<DataStandardCatalogMapper, DataStandardCatalog>
                                            implements DataStandardCatalogService {

    @Override
    public Page<DataStandardCatalogVO> pageAll(PageDTO pageDTO) {
        Page<DataStandardCatalog> dataStandardCatalogPage = new Page<>(pageDTO.getPageNumber(), pageDTO.getPageSize());
        Page<DataStandardCatalog> page = this.page(dataStandardCatalogPage);
        Page<DataStandardCatalogVO> dataStandardCatalogVOPage = new Page<>();
        dataStandardCatalogVOPage.setRecords(BeanConverter.convertList(page.getRecords(),DataStandardCatalogVO.class));
        dataStandardCatalogVOPage.setSize(page.getSize());
        dataStandardCatalogVOPage.setTotal(page.getTotal());
        dataStandardCatalogVOPage.setCurrent(page.getCurrent());
        dataStandardCatalogVOPage.setPages(page.getPages());
        return dataStandardCatalogVOPage;
    }

    @Override
    public Page<DataStandardCatalogVO> pageAllByQuery(DataStandardCatalogDTO dataStandardCatalogDTO) {
        Page<DataStandardCatalog> page = new Page<>(dataStandardCatalogDTO.getPageNumber(), dataStandardCatalogDTO.getPageSize());
        LambdaQueryWrapper<DataStandardCatalog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataStandardCatalog::getSourceOrgan,dataStandardCatalogDTO.getSourceOrgan())
                .eq(DataStandardCatalog::getStandardState,dataStandardCatalogDTO.getStandardState())
                .like(DataStandardCatalog::getStandardCode,dataStandardCatalogDTO.getStandardCode())
                .like(DataStandardCatalog::getCName,dataStandardCatalogDTO.getCName())
                .like(DataStandardCatalog::getEName,dataStandardCatalogDTO.getEName());
        Page<DataStandardCatalog> resultPage = this.page(page, wrapper);
        Page<DataStandardCatalogVO> returnPage = new Page<>();
        returnPage.setRecords(BeanConverter.convertList(resultPage.getRecords(),DataStandardCatalogVO.class));
        returnPage.setSize(returnPage.getSize());
        returnPage.setTotal(resultPage.getTotal());
        returnPage.setCurrent(returnPage.getCurrent());
        returnPage.setPages(resultPage.getPages());
        return returnPage;
    }

    @Override
    public ClickOnStandardLableVO ClickOnOne(Integer id) {
        LambdaQueryWrapper<DataStandardCatalog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DataStandardCatalog::getId,id);
        DataStandardCatalog one = this.getOne(wrapper);
        ClickOnStandardLableVO clickOnStandardLableVO = new ClickOnStandardLableVO();
        clickOnStandardLableVO.setStandardCode(one.getStandardCode());
        clickOnStandardLableVO.setCName(one.getCName());
        clickOnStandardLableVO.setEName(one.getEName());
        clickOnStandardLableVO.setStandardDesc(one.getStandardDesc());
        clickOnStandardLableVO.setSourceOrgan(one.getSourceOrgan());
        clickOnStandardLableVO.setNumberType(one.getNumberType());
        clickOnStandardLableVO.setCode(one.getCode());
        clickOnStandardLableVO.setDefaultNum(one.getDefaultNum());
        return clickOnStandardLableVO;
    }

    @Override
    public Boolean addDataStandard(AddDataStandardCatalogDTO addDataStandardCatalogDTO) {
        DataStandardCatalog dataStandardCatalog = new DataStandardCatalog();
        dataStandardCatalog.setCName(addDataStandardCatalogDTO.getCName());
        dataStandardCatalog.setEName(addDataStandardCatalogDTO.getEName());
        dataStandardCatalog.setStandardDesc(addDataStandardCatalogDTO.getStandardDesc());
        dataStandardCatalog.setSourceOrgan(addDataStandardCatalogDTO.getSourceOrgan());
        dataStandardCatalog.setNumberType(addDataStandardCatalogDTO.getNumberType());
        dataStandardCatalog.setLength(10);
        dataStandardCatalog.setNumAccuracy(2);
        dataStandardCatalog.setDefaultNum("01");
        dataStandardCatalog.setMin(1);
        dataStandardCatalog.setMax(99999);
        dataStandardCatalog.setCode(1);
        dataStandardCatalog.setIsEnable(0);
        dataStandardCatalog.setStandardState(0);
        dataStandardCatalog.setCreateTime(LocalDateTime.now());
        dataStandardCatalog.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<DataStandardCatalog> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(DataStandardCatalog::getId).last("LIMIT 1");;
        DataStandardCatalog one = this.getOne(wrapper);
        String numString = one.getStandardCode().substring(2, 7);
        Integer i = Integer.valueOf(numString);
        int num = i + 1;
        String resultCode = null;
        switch (String.valueOf(num).length()){
            case 1: resultCode = "BZ0000" + num;break;
            case 2: resultCode = "BZ000" + num;break;
            case 3: resultCode = "BZ00" + num;break;
            case 4: resultCode = "BZ0" + num;break;
            case 5: resultCode = "BZ" + num;break;
        }
        dataStandardCatalog.setStandardCode(resultCode);
        if (this.save(dataStandardCatalog)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updateStatic(Integer id) {
        DataStandardCatalog one = this.getById(id);
        switch (one.getStandardState()){
            case 0, 2:
                one.setStandardState(1);break;
            case 1:
                one.setStandardState(2);break;
        }
        if(this.updateById(one)){
            return true;
        }else {
            return false;
        }
    }

}
